/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mobiwebinc.compconn.client;

import com.mobiwebinc.compconn.communication.IOHandler;
import com.mobiwebinc.compconn.configuration.ClientConfiguration;
import com.mobiwebinc.compconn.events.EventHandler;
import com.mobiwebinc.compconn.events.command.Command;
import com.mobiwebinc.compconn.events.command.Decoder;
import java.awt.Rectangle;

/**
 *
 * @author suraj
 */
public class ConfigGrabber implements EventHandler {
    private IOHandler io;
    private ClientConfiguration clientConfiguration;

    public ConfigGrabber(IOHandler io, ClientConfiguration clientConfiguration) {
        this.io = io;
        this.clientConfiguration = clientConfiguration;
    }

    public void grabConfig() {
        boolean loading = true;
        while (loading && getIo().isConnected()) {
            String cmd = getIo().readMsg();
            if (isSupported(cmd)) {
                loading = process(cmd);
            }
        }
    }

    private boolean process(String cmd) {
        if (cmd.startsWith(Command.CONFIG_NAME)) {
            getClientConfiguration().setName(Decoder.stringParameter(cmd));
        } else if (cmd.startsWith(Command.CONFIG_SIZE)) {
            int[] params = Decoder.intParameters(cmd);
            getClientConfiguration().setScreenRect(new Rectangle(0, 0, params[0], params[1]));
        } else if (cmd.startsWith(Command.CONFIG_DISPLAY_QUALITY)) {
            double[] params = Decoder.doubleParameters(cmd);
            getClientConfiguration().setImageQuality(params[0]);
            getIo().setupImageWriter((float) getClientConfiguration().getImageQuality());
        } else if (cmd.startsWith(Command.CONFIG_SHOW_MOUSE_POINTER)) {
            String[] params = Decoder.stringParameters(cmd);
            getClientConfiguration().setCursorVisible(params[0].equals("1"));
        } else if (cmd.startsWith(Command.CONFIG_TAP_ZOOM_SENSITIVITY)) {
            double[] params = Decoder.doubleParameters(cmd);
            getClientConfiguration().setTapZoom(params[0]);
        } else if (cmd.startsWith(Command.CONFIG_ZOOM_SENSITIVITY)) {
            double[] params = Decoder.doubleParameters(cmd);
            getClientConfiguration().setZoomLevel(params[0]);
        } else if (cmd.startsWith(Command.CONFIG_TERM)) {
            return false;
        } else if(cmd.startsWith(Command.CLIENT_TERM)) {
            getIo().disconnect();
            return false;
        }
        return true;
    }

    public void handleRequest(String cmd) {
        process(cmd);
    }

    public boolean isSupported(String cmd) {
        return cmd.startsWith(Command.CONFIG_PREFIX) || cmd.startsWith(Command.CLIENT_TERM);
    }

    /**
     * @return the io
     */
    public IOHandler getIo() {
        return io;
    }

    /**
     * @return the clientConfiguration
     */
    public ClientConfiguration getClientConfiguration() {
        return clientConfiguration;
    }
}
